import axios from 'axios'

//统一封装 axios 实例
const instance = axios.create({
    baseURL: "/api", //用来处理跨域的一级路径(处理后变成空字符串，具体配置在 config/index.js 下)
    timeout: 5000 //5s 超时
});

instance.interceptors.request.use(config => {
    //在请求头中加入 Token
    const token = localStorage.getItem("token");
    if (token == null || token == '') {
        return config;
    }
    config.headers.set("token", token);
    return config;
});

instance.interceptors.response.use(success => {
    console.log("成功：响应拦截器");
    return success;
}, error => {
    console.log("失败：响应拦截器");
    return Promise.reject(error);
});

export default instance;

